/*
 * Created on 28/set/07
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package it.unibo.deis.lia.mmhc;

import it.unibo.deis.lia.networkInterface.*;
import it.unibo.deis.lia.mac.nip.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import java.net.*;
import java.util.*;

public class PeerConnectorGUI_OLD extends Thread implements ActionListener{

    private JFrame frame=null;
    private JTextField textFieldInternal=null;
    private JTextField textFieldExternal=null;
    private GenericNetworkInterface internalInterface=null;
    private GenericNetworkInterface externalInterface=null;
    
	public void run(){
    	try{
			//JFrame
			frame=new InterfacesJFrame(this);
			frame.setTitle("PeerConnectorGUI");
			frame.pack();
	        frame.setLocation(50, 50);
	        frame.setVisible(true);
    	}
        catch(Exception e){
        	e.printStackTrace();
        }
	}
	
	private void refresh()throws Exception{
		frame.setVisible(false);
		/*JFrame */frame=new InterfacesJFrame(this);
		frame.setTitle("PeerConnectorGUI");
		frame.pack();
        frame.setLocation(50, 50);
        frame.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent event){
		try{
			JButton source=(JButton)(event.getSource());
			String text=source.getText();
			if(text.equals("refresh interfaces")){
	        	System.out.println("PeerConnectorGUI refresh interfaces");
				/*frame.setVisible(false);
				frame=new InterfacesJFrame(this);
				frame.pack();
		        frame.setLocation(50, 50);
		        frame.setVisible(true);*/
				refresh();
			}
			else if(text.equals("SET connector role")){
	        	System.out.println("PeerConnectorGUI SET connector role");
				String internalInterfacDescription=textFieldInternal.getText();
				String externalInterfacDescription=textFieldExternal.getText();
				
				//GenericNetworkInterface internalInterface=null;
				//GenericNetworkInterface externalInterface=null;
				externalInterface=null;
		        Vector<GenericNetworkInterface> interfacesIN=NetworkInterfaceProvider.getInstance().getAvailableInterfaces();
	        	System.out.println("PeerConnectorGUI interfacesIN: "+interfacesIN);
		        for(int i=0; i<interfacesIN.size() && internalInterface==null; i++){
		        	GenericNetworkInterface gni=interfacesIN.elementAt(i);
		        	System.out.println("PeerConnectorGUI gni: "+gni);
		        	String description=gni.getDescription();
		        	System.out.println("PeerConnectorGUI description: "+description);
		        	System.out.println("PeerConnectorGUI internalInterfacDescription: "+externalInterfacDescription);
		            if(description.equals(internalInterfacDescription)){
		            	internalInterface=gni;
		            }
		            /*if(description.equals(externalInterfacDescription)){
		            	externalInterface=gni;
		            }*/
		        }
		        
		        Vector<GenericNetworkInterface> interfacesEXT=GenericNetworkInterface.getInterfaces();
				//Vector<GenericNetworkInterface> interfaces=NetworkInterfaceProvider.getInstance().getAvailableInterfaces();
		        for(int i=0; i<interfacesEXT.size() && externalInterface==null; i++){
		        	GenericNetworkInterface gni=interfacesEXT.elementAt(i);
		        	String description=gni.getDescription();
		            /*if(description.equals(internalInterfacDescription)){
		            	internalInterface=gni;
		            }*/
		        	System.out.println("PeerConnectorGUI description: "+description);
		        	System.out.println("PeerConnectorGUI externalInterfacDescription: "+externalInterfacDescription);
		            if(description.equals(externalInterfacDescription)){
		            	externalInterface=gni;
		            }
		        }
		        
		        //IEEE80211 ieee80211=new IEEE80211();
		        //Bluetooth bluetooth=new Bluetooth();
		        
		        if(internalInterface!=null){
		        	if(internalInterface instanceof IEEE80211){
		        		IEEE80211 ieee80211=(IEEE80211)internalInterface;
		        		ieee80211.setConnectorRole(externalInterface);
		        	}
		        	else if(internalInterface instanceof Bluetooth){
		        		Bluetooth bluetooth=(Bluetooth)internalInterface;
		        		bluetooth.setConnectorRole(externalInterface);
		        	}
		        	else{
		        		throw new Exception("Interface not supported: "+internalInterface);
		        	}
		        }
		        
				/*frame.setVisible(false);
				frame=new InterfacesJFrame(this);
				frame.pack();
		        frame.setLocation(50, 50);
		        frame.setVisible(true);*/
				refresh();
			}
			else if(text.equals("RESET connector role")){
	        	System.out.println("PeerConnectorGUI RESET connector role");
				if(internalInterface!=null){
		        	if(internalInterface instanceof IEEE80211){
		        		IEEE80211 ieee80211=(IEEE80211)internalInterface;
			        	  
						DatagramSocket ds=new DatagramSocket(3333);//,InetAddress.getByName(ipAddress));
						byte[] buffer=("terminato peer connector "+internalInterface).getBytes();
						DatagramPacket dp=new DatagramPacket(buffer,buffer.length,InetAddress.getByName("137.204.57.176"),3333);
						System.out.println("PeerConnectorGUI sending "+new String(buffer));
						ds.send(dp);
						ds.close();
						
		        		ieee80211.resetConnectorRole();
		        	}
		        	else if(internalInterface instanceof Bluetooth){
		        		Bluetooth bluetooth=(Bluetooth)internalInterface;
		        		bluetooth.resetConnectorRole();
		        	}
		        	else{
		        		throw new Exception("Interface not supported: "+internalInterface);
		        	}
					
					internalInterface=null;
					externalInterface=null;
		        }
				
				/*frame.setVisible(false);
				frame=new InterfacesJFrame(this);
				frame.pack();
		        frame.setLocation(50, 50);
		        frame.setVisible(true);*/
				refresh();
			}
			else{
				System.out.println("PeerConnectorGUI errror! "+text);
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	private class InterfacesJFrame extends JFrame{
		public InterfacesJFrame(PeerConnectorGUI_OLD peerConnectorGUI) throws Exception{
			JFrame frame=this;
	        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	        
	        Container pane=frame.getContentPane();
	        pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
	        
	        JButton button1 = new JButton("refresh interfaces");
	        button1.addActionListener(peerConnectorGUI);
	        button1.setAlignmentX(Component.CENTER_ALIGNMENT);
	        pane.add(button1);
	        
	        /**/Vector<GenericNetworkInterface> interfaces=GenericNetworkInterface.getInterfaces();
	        for(int i=0;i<interfaces.size();i++){
	        	GenericNetworkInterface gni=interfaces.elementAt(i);
	        	String description=gni.getDescription();
	        	
	            JLabel label = new JLabel(i+": "+description);
	            label.setAlignmentX(Component.CENTER_ALIGNMENT);
	            pane.add(label);
	        }/**/
	        
	       /* Enumeration<NetworkInterface> interfaces=java.net.NetworkInterface.getNetworkInterfaces();
	        while(interfaces.hasMoreElements()){
	        	NetworkInterface ni=interfaces.nextElement();
	        	String description=ni.getDisplayName();
	        	
	            JLabel label = new JLabel(description);
	            label.setAlignmentX(Component.CENTER_ALIGNMENT);
	            pane.add(label);
	        }/**/
	        
	        JLabel label1 = new JLabel("===================================");
            label1.setAlignmentX(Component.CENTER_ALIGNMENT);
            pane.add(label1);
            
            
	        
	        JButton button2 = new JButton("SET connector role");
	        button2.addActionListener(peerConnectorGUI);
	        button2.setAlignmentX(Component.CENTER_ALIGNMENT);
	        pane.add(button2);
	        
	        /*JTextField*/ textFieldInternal = new JTextField("internal interface");
	        textFieldInternal.setAlignmentX(Component.CENTER_ALIGNMENT);
	        pane.add(textFieldInternal);
	        /*JTextField*/ textFieldExternal = new JTextField("external interface");
	        textFieldExternal.setAlignmentX(Component.CENTER_ALIGNMENT);
	        pane.add(textFieldExternal);
	        
	        
	        JLabel label2 = new JLabel("===================================");
            label2.setAlignmentX(Component.CENTER_ALIGNMENT);
            pane.add(label2);
	        

	        JButton button3 = new JButton("RESET connector role");
	        button3.addActionListener(peerConnectorGUI);
	        button3.setAlignmentX(Component.CENTER_ALIGNMENT);
	        pane.add(button3);
	        
            JLabel internal = new JLabel("internal: "+internalInterface);
            internal.setAlignmentX(Component.CENTER_ALIGNMENT);
            pane.add(internal);
            
            JLabel external = new JLabel("external: "+externalInterface);
            external.setAlignmentX(Component.CENTER_ALIGNMENT);
            pane.add(external);
            
	        frame.pack();
	        frame.setLocation(50, 50);
	        frame.setVisible(true);
		}
	}
}
